package com.oa.official.controller;

import com.oa.official.service.SysRoleService;
import com.oa.official.entity.system.SysRole;
import com.oa.official.response.Result;

import com.oa.official.vo.system.AssginRoleVo;
import com.oa.official.vo.system.SysRoleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "角色管理")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {

    @Resource
    private SysRoleService sysRoleService;

    @ApiOperation(value = "角色分页列表")
    @GetMapping("/{page}/{limit}")
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    public Result list(@PathVariable(value = "page")Integer page,
                       @PathVariable(value = "limit")Integer limit,
                       SysRoleQueryVo sysRoleQueryVo){
        return sysRoleService.list(page, limit, sysRoleQueryVo);
    }

    @ApiOperation(value = "新增角色")
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    public Result save(@RequestBody SysRole sysRole){
        return sysRoleService.save(sysRole);
    }

    @ApiOperation(value = "根据角色id查询")
    @GetMapping("/get/{id}")
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    public Result findByRoleId(@PathVariable(value = "id")Long id){
        return sysRoleService.findByRoleId(id);
    }

    @ApiOperation(value = "修改角色")
    @PutMapping("/update")
    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    public Result update(@RequestBody SysRole sysRole){
        return sysRoleService.update(sysRole);
    }


    @ApiOperation(value = "删除角色")
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    public Result deleteById(@PathVariable(value = "id")Long id){
        return sysRoleService.deleteById(id);
    }


    @ApiOperation(value = "批量删除角色")
    @DeleteMapping("/batchRemove")
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    public Result batchRemove(@RequestBody List<Long> ids){
        return sysRoleService.batchRemove(ids);
    }

    @ApiOperation(value = "获取角色")
    @GetMapping ("/toAssign/{userId}")
    public Result toAssign(@PathVariable(value = "userId")Long userId) {
        return sysRoleService.toAssign(userId);
    }

    @ApiOperation(value = "为用户分配角色")
    @PostMapping ("/doAssign")
    public Result doAssign(@RequestBody AssginRoleVo assginRoleVo) {
        return sysRoleService.doAssign(assginRoleVo);
    }
}